package com.tnxst.phexamination.callback;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.tnxst.phexamination.R;
import com.tnxst.phexamination.utils.GsonUtils;
import com.tnxst.phexamination.utils.StringUtils;
import com.youth.banner.util.LogUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public abstract class AppCallBack extends Callback<String> {
    private Context mContext;
    private boolean mIsShowDialog;//是否开启动画
    private Request mRequest;
    private AlertDialog show;
    public static Gson mGson = new Gson();
    private AnimationDrawable ad;

    public AppCallBack(Context context, boolean isShowDialog) {
        mContext = context;
        mIsShowDialog = isShowDialog;
    }

    public AppCallBack(Context mContext) {
        this(mContext, true);
    }


    @Override
    public String parseNetworkResponse(Response response, int id) throws Exception {
        try {
            String r = response.body().string();
            r = StringUtils.replace(r);
            //解析json,返回bean对象
            Log.d("Success:","====>>>"+r);
            return r;
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    public void onBefore(Request request, int id) {
        //这里开启动画
        mRequest = request;
        if (mIsShowDialog && mContext != null) {
            if (show == null) {
                final AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext, R.style.AlertDialog);
                View view = View.inflate(mContext, R.layout.login_loading, null);
                alertDialog
                        .setView(view)
                        .create();
                show = alertDialog.show();
                show.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                show.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
                TextView textView = show.findViewById(R.id.imageView1);
                textView.setText("正在同步！请稍后！");
                ad = (AnimationDrawable) textView.getCompoundDrawables()[1];
                show.setCancelable(false);
            }
            if (mContext instanceof Activity && !((Activity) mContext).isFinishing()) {
                show.show();
                ad.start();
            }
        }
        super.onBefore(request, id);
    }

    @Override
    public void onAfter(int id) {
        //这里结束动画
        try {
            if (show != null && show.isShowing()) {
                show.dismiss();
                ad.stop();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onAfter(id);
    }

    @Override
    public void onError(Call call, Exception e, int id) {
        Toast.makeText(mContext,"同步失败！\n失败信息：" + e.getMessage(), Toast.LENGTH_LONG);
        Log.d("onError:","====>>>"+e.getMessage());
    }
}
